home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 32 / Amiga Format AFCD32 (Nov 1998, Issue 117).iso / -seriously_amiga- / programming / other / hrtmon / hrtmonload.c < prev    next >
C/C++ Source or Header  |  1998-08-10  |  5KB  |  262 lines

  1.  
  2. /*
  3.  
  4.   Filename:    HRTmonLoad.c
  5.   Author:      Alain Malek
  6.   Description: Loading routines used by HRTmon and HRTmonPrefs
  7.  
  8. */
  9.  
  10. #include "HRTmonLoad.h"
  11.  
  12. #define nb_keyboard 4        // nb keyboards selectable in HRTmonPrefs
  13.  
  14. /*******************************************************************/
  15.  
  16. static char A1200;
  17. static char CD32;
  18. static char AGA;
  19.  
  20. int execver;
  21.  
  22. struct GfxBase *GfxBase;
  23.  
  24. // simple Loadseg for HRTmon file
  25. // load from memory to ABS place
  26.  
  27.  
  28. CONFIG config;
  29.  
  30. // HRTmon header
  31.  
  32. typedef struct {
  33.     char jmps[20];
  34.     unsigned int mon_size;
  35.     unsigned short col0, col1;
  36.     char right;
  37.     char keyboard;
  38.     char key;
  39.     char ide;
  40.     char a1200;
  41.     char aga;
  42.     char insert;
  43.     char delay;
  44.     char lview;
  45.     char cd32;
  46.     char screenmode;
  47.     char vbr;
  48.     char entered;
  49.     char align;
  50. } HRTCFG;
  51.  
  52.  
  53. /*******************************************************************/
  54.  
  55. void set_default()
  56. {
  57.     strcpy(config.id,"HRT");
  58.     config.version = CFGVER;
  59.     config.address = 0x1c000;
  60.     config.col0 = 0x000;
  61.     config.col1 = 0xeee;
  62.     config.abs = TRUE;
  63.     config.autoa = TRUE;
  64.     config.right = TRUE;
  65.     config.key = TRUE;
  66.     config.ide = FALSE;
  67.     config.insert = FALSE;
  68.     config.lview = TRUE;
  69.     config.vbr = TRUE;
  70.     config.keyboard = 0;
  71.     config.delay = 15;
  72.     config.screenmode = SCREEN_PAL;
  73. }
  74.  
  75. /*******************************************************************/
  76.  
  77. void LoadInit()
  78. {
  79.     struct ExecBase *SysBase=(int*)*((int*)4);
  80.     BPTR cf;
  81.     int bread;
  82.  
  83.     GfxBase = (struct GfxBase *) OpenLibrary(GRAPHICSNAME,0);
  84.     AGA = (GfxBase->ChipRevBits0) & 8;    /* Test LISA bit */
  85.  
  86.     A1200 = (FindName(&(SysBase->DeviceList), "carddisk.device") != 0);
  87.     CD32 = (FindName(&(SysBase->DeviceList), "elsat.device") != 0);
  88.     execver = SysBase->LibNode.lib_Version;
  89.  
  90.     set_default();
  91.  
  92.     if ( cf=Open("ENV:HRTmon.prefs",MODE_OLDFILE) ) {
  93.         bread = Read(cf, &config, sizeof(CONFIG));
  94.         if (bread != sizeof(CONFIG))
  95.             config.version = 0;
  96.         Close(cf);
  97.         if(strcmp(config.id, "HRT") || config.version != CFGVER)
  98.             set_default();
  99.         if (config.screenmode >= SCREEN_MAX)
  100.             config.screenmode = SCREEN_PAL;
  101.     }
  102.  
  103. }
  104.  
  105.  
  106. /*******************************************************************/
  107.  
  108. void wbmain()
  109. {
  110.     main(1, NULL);
  111. }
  112.  
  113. /*******************************************************************/
  114.  
  115. int Load(char *path)
  116. {
  117.     char *Buffer;
  118.     BPTR filelock,filehandle;
  119.     struct FileInfoBlock infoblock;
  120.     int autoaddr;
  121.     char hrtname[512+32];
  122.     char tmptxt[256];
  123.  
  124.     strcpy(hrtname, path);
  125.     strcat(hrtname, "HRTmon.data");
  126.  
  127.     if ( (filelock=Lock(hrtname,ACCESS_READ)) == NULL ) {    // Lock file
  128.         strcpy(hrtname, config.path);
  129.         strcat(hrtname, "/HRTmon.data");
  130.         if ( (filelock=Lock(hrtname,ACCESS_READ)) == NULL ) {
  131.             printf("Can't open file HRTmon.data\n");
  132.             return (TRUE);
  133.         }
  134.     }
  135.  
  136.     if ( Examine(filelock,&infoblock) == FALSE ) {        // Examine file
  137.         printf("Can't open file HRTmon.data\n");
  138.         UnLock(filelock);
  139.         return (TRUE);
  140.     }
  141.  
  142.     UnLock(filelock);                    // UnLock file
  143.  
  144.     if ( (Buffer=AllocMem(infoblock.fib_Size,MEMF_ANY)) == NULL ) {
  145.         printf("Not enough memory to proceed.\n");
  146.         return (TRUE);
  147.     }
  148.  
  149.     if ( filehandle=Open(hrtname,MODE_OLDFILE) ) {
  150.         Read(filehandle,Buffer,infoblock.fib_Size);        // Read file
  151.         Close(filehandle);
  152.     }
  153.     else {                        // if Can't Open
  154.         printf("Can't open file HRTmon.data\n");
  155.         return (TRUE);
  156.     }
  157.  
  158.     if (config.autoa)
  159.         autoaddr = 0;
  160.     else
  161.         autoaddr = config.address;
  162.  
  163.     if ( LoadABS(Buffer,&autoaddr) == FALSE ) {
  164.         FreeMem(Buffer,infoblock.fib_Size);
  165.         printf("Installation of HRTmon failed.\n");
  166.         return (TRUE);
  167.     }
  168.  
  169.     FreeMem(Buffer,infoblock.fib_Size);
  170.  
  171.     DoJSR((unsigned int)autoaddr+8);        // Init HRTmon
  172.  
  173.     sprintf(tmptxt,"HRTmon installed at $%x.\n",autoaddr);
  174.     ShowReq(tmptxt,"OK");
  175.  
  176.     return (FALSE);
  177. }
  178.  
  179. /*******************************************************************/
  180. // Only for HRTmon.data file ! (single simple code Hunk)
  181. // Load file from 'memory' to 'place' and AllocAbs place if 'abs'=TRUE.
  182.  
  183. int LoadABS(int *memory,int *place)
  184. {
  185.     int *p,len,i;
  186.     int *base;
  187.     char *base2;
  188.     HRTCFG *hrtcfg;
  189.  
  190.  
  191.     if ( (*(memory+6) != 0x3e9) || (*memory != 0x3f3) )
  192.         return (FALSE);
  193.  
  194.     len=*(memory+7);
  195.  
  196.     if (*place == 0) {
  197.         if (execver >= 39)
  198.             *place = AllocMem(len*4, MEMF_PUBLIC | MEMF_REVERSE);
  199.         else
  200.             *place = AllocMem(len*4, MEMF_PUBLIC);
  201.  
  202.         if (*place == 0) {
  203.             printf("Not enough memory !\n");
  204.             return(FALSE);
  205.         }
  206.     }
  207.  
  208.     if (config.abs && (!config.autoa)) {
  209.         if ( AllocAbs(len*4,(APTR)*place) == 0 ) {
  210.         printf("Can't AllocAbs memory at address.\n");
  211.         return (FALSE);
  212.         }
  213.     }
  214.  
  215.     CopyMemQuick( (ULONG*)memory+8, (ULONG*)*place, len*4);
  216.  
  217.     if (config.abs || config.autoa)
  218.         ((ULONG*)*place)[5]=len*4;
  219.     else
  220.         ((ULONG*)*place)[5]=0;
  221.  
  222.  
  223.     hrtcfg = (HRTCFG*)(*place);
  224.  
  225.     hrtcfg->col0 = config.col0;
  226.     hrtcfg->col1 = config.col1;
  227.     hrtcfg->right = config.right;
  228.     hrtcfg->keyboard = config.keyboard;
  229.     hrtcfg->key = config.key;
  230.     hrtcfg->ide = config.ide;
  231.     hrtcfg->a1200 = A1200;
  232.     hrtcfg->aga = AGA;
  233.     hrtcfg->insert = config.insert;
  234.     hrtcfg->delay = config.delay;
  235.     hrtcfg->lview = config.lview;
  236.     hrtcfg->cd32 = CD32;
  237.     hrtcfg->screenmode = config.screenmode;
  238.     hrtcfg->vbr = config.vbr;
  239.  
  240.  
  241.     p=memory+8+len+3;        //p points on reloc_32 hunk offsets
  242.     len=*(p-2);
  243.  
  244.     base2=(char*)*place;
  245.  
  246.     for ( i=0; i<len ; i++ ) {
  247.  
  248.         base=&base2[*p];
  249.         *base = *base + *place;
  250.         p++;
  251.     }
  252.  
  253.     if (execver >= 37)
  254.         CacheClearU();
  255.  
  256.     return (TRUE);
  257. }
  258.  
  259. /*******************************************************************/
  260.  
  261.  
  262.